#!/bin/sh

# 启动 php-fpm
echo "Starting PHP-FPM..."
php-fpm &
PHP_FPM_PID=$!  # 获取 php-fpm 进程 ID

# 检查 php-fpm 是否启动成功
if ! kill -0 $PHP_FPM_PID 2>/dev/null; then
  echo "PHP-FPM failed to start."
  exit 1
fi

# 启动 nginx
echo "Starting Nginx..."
nginx -g "daemon off;" &
NGINX_PID=$!  # 获取 nginx 进程 ID

# 检查 nginx 是否启动成功
if ! kill -0 $NGINX_PID 2>/dev/null; then
  echo "Nginx failed to start."
  exit 1
fi

# 等待任一进程退出
wait -n $PHP_FPM_PID $NGINX_PID
EXIT_STATUS=$?  # 获取退出状态码

# 处理退出状态
if [ $EXIT_STATUS -ne 0 ]; then
  echo "One of the services failed. Exit code: $EXIT_STATUS"
  echo "Hanging container for debugging..."
  tail -f /dev/null  # 挂起容器
else
  echo "Services stopped gracefully."
fi